#define _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void Bubbsort(int* p, int sz) {
	int i = 0;
	for (i = 0; i < sz - 1; i++) {
		int swap = 1;
		int j = 0;
		for (j = 0; j < sz - 1 - i; j++) {
			if (p[j] > p[j + 1]) {
				int tmp = p[j];
				p[j] = p[j + 1];
				p[j + 1] = tmp;
				swap = 0;
			}
			if (swap) {
				break;
			}
		}
	}
}

void Print(int* p, int sz) {
	for (int i = 0; i < sz; i++) {
		printf("%d ", p[i]);
	}
	printf("\n");
}

void test() {
	int arr[10] = { 9,8,7,6,0,1,2,3,4,5 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	Bubbsort(arr, sz);
	Print(arr, sz);
}

int com_int(const void* p1, const void* p2) {
	return *(int*)p1 - *(int*)p2;
}

void test1() {
	int arr[10] = { 9,8,7,6,0,1,2,3,4,5 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	qsort(arr, sz, sizeof(arr[0]), com_int);
	Print(arr, sz);
}

struct student {
	char name[20];
	int age;
};

int com_stu_by_name(const void* p1, const void* p2) {
	return strcmp(((struct student*)p1)->name, ((struct student*)p2)->name);
}

void test2() {
	struct student stu1[3] = { {"zhangsan",18},{"lisi",23},{"wangwu",32} };
	int sz = sizeof(stu1) / sizeof(stu1[0]);
	qsort(stu1, sz, sizeof(stu1[0]), com_stu_by_name);
	for (int i = 0; i < sz; i++) {
		printf("%s %d\n", stu1[i].name, stu1[i].age);
	}
}

void swap(char* buf1, char* buf2, int so) {
	for (int i = 0; i < so; i++) {
		char tmp = buf1[i];
		buf1[i] = buf2[i];
		buf2[i] = tmp;
	}
}

int Com_int(const void*p1, const void*p2) {
	return *(int*)p1 - *(int*)p2;
}

void Bubbsort1(void* base, size_t sz,size_t so,int (*com)(const void*,const void*)) {
	char* p = (char*)base;
	int i = 0;
	for (i = 0; i < sz - 1; i++) {
		int flag = 1;
		int j = 0;
		for (j = 0; j < sz - 1 - i; j++) {
			if (com(p+j*so,p+(j+1)*so)>0){
				swap(p + j * so, p + (j + 1) * so, so);
				flag = 0;
			}
			if (flag) {
				break;
			}
		}
	}
}

void test3() {
	struct student stu1[3] = { {"zhangsan",18},{"lisi",23},{"wangwu",32} };
	int sz = sizeof(stu1) / sizeof(stu1[0]);
	Bubbsort1(stu1, sz, sizeof(stu1[0]),Com_int);
	for (int i = 0; i < sz; i++) {
		printf("%s %d\n", stu1[i].name, stu1[i].age);
	}
}

int main() {
	//test();
	//test1();
	//test2();
	test3();
	return 0;
}